﻿
using Minio;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using System.Reactive.Linq;



public class QhseFileImplService : IQhseFileService
    {
        private readonly MinioClient _minioClient;
    private readonly string bucketUse;
        public QhseFileImplService(IConfiguration configuration)
        {
            Console.WriteLine(configuration["MinIO:EndPoint"]+ configuration["MinIO:AccessKey"]+ configuration["MinIO:SecretKey"]);
        this._minioClient = new MinioClient().WithEndpoint(configuration["MinIO:EndPoint"]).WithCredentials(ConfigHelper.ConfigDecrypt(configuration["MinIO:AccessKey"]), ConfigHelper.ConfigDecrypt(configuration["MinIO:SecretKey"])).Build();
        this.bucketUse = configuration["MinIO:Bucket"];
        }

        public async Task PutAsync(string bucket, string fileName, IFormFile formFile)
        {
            bucket = bucketUse;
            bool found = await _minioClient.BucketExistsAsync(new BucketExistsArgs().WithBucket(bucket));
        if (!found) await _minioClient.MakeBucketAsync(new MakeBucketArgs().WithBucket(bucket));
                try { await _minioClient.PutObjectAsync(new PutObjectArgs().WithBucket(bucket).WithObject(fileName).WithObjectSize(formFile.Length).WithContentType("image/jpg").WithStreamData(formFile.OpenReadStream())); }
                catch { throw new Exception("文件保存失败"); }
        }
    public async Task PutPDFAsync(string bucket, string fileName, IFormFile formFile)
    {
        bucket = bucketUse;
        bool found = await _minioClient.BucketExistsAsync(new BucketExistsArgs().WithBucket(bucket));
        if (!found) await _minioClient.MakeBucketAsync(new MakeBucketArgs().WithBucket(bucket));
        try { await _minioClient.PutObjectAsync(new PutObjectArgs().WithBucket(bucket).WithObject(fileName).WithObjectSize(formFile.Length).WithContentType("application/pdf").WithStreamData(formFile.OpenReadStream())); }
        catch { throw new Exception("文件保存失败"); }
    }
    public async Task GetAsync(MemoryStream stream, string bucket, string fileName)
        {
        bucket = bucketUse;
        bool found = await _minioClient.BucketExistsAsync(new BucketExistsArgs().WithBucket(bucket));
            if (!found) throw new Exception("未找到目标文件，无法下载！");

        Console.WriteLine(bucket + fileName);
            try { await _minioClient.GetObjectAsync(new GetObjectArgs().WithBucket(bucket).WithObject(fileName).WithCallbackStream(cs => { cs.CopyTo(stream); })); }
            catch { throw new Exception("未找到目标文件，无法下载！"); }
        }

        public async Task RemoveAsync(string bucket, string fileName)
        {
        bucket = bucketUse;
        bool found = await _minioClient.BucketExistsAsync(new BucketExistsArgs().WithBucket(bucket));
            if (found)
                try
                {
                    await _minioClient.RemoveObjectAsync(new RemoveObjectArgs().WithBucket(bucket).WithObject(fileName));
                    Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss}\t已删除无用文件{fileName}");
                }
                catch { throw new Exception("未找到目标文件，删除失败！"); }
        }



    public void Dispose()
        {
            _minioClient?.Dispose();
            GC.SuppressFinalize(this);
        }

    }
